package com.cssy.yd.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.logic.entity.LogicOtherInwhItemBatchDO;
import com.cssy.yd.logic.entity.LogicOtherInwhItemDO;
import com.cssy.yd.logic.entity.LogicOtherOutwhDO;
import com.cssy.yd.logic.entity.LogicOtherOutwhItemBatchDO;
import com.cssy.yd.logic.entity.LogicOtherOutwhItemDO;
import com.cssy.yd.logic.service.ILogicOtherOutwhItemService;
import com.cssy.yd.logic.service.ILogicOtherOutwhService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/yd/logic/otherOutwh")
public class LogicOtherOutwhController extends
		BaseController<LogicOtherOutwhDO> {

	private static final String MANAGER = "/yd/logic/otheroutwh/Manager";
	private static final String EIDT = "/yd/logic/otheroutwh/Operation";// 添加、修改、显示页面
//	private static final String ITEMMANAGER = "/yd/logic/otheroutwh/ItemManager";//添加、修改、显示页面
	
	
	@Resource(name = "logicOtherOutwhService")
	private ILogicOtherOutwhService logicOtherOutwhService;

	@Resource(name = "logicOtherOutwhItemService")
	private ILogicOtherOutwhItemService logicOtherOutwhItemService;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}
//	@RequestMapping(value="itemManager")
//	public String itemManager(HttpServletRequest request, Model model)
//			throws Exception {
//		String itemdata =request.getParameter("data");
//		String billstatus =request.getParameter("billstatus");
//		String whCode =request.getParameter("whCode");
//		LogicOtherOutwhItemDO lOtherOutwhItemDO=JSONArray.parseObject(new String(itemdata.getBytes("ISO-8859-1"),"UTF-8"), LogicOtherOutwhItemDO.class);
//		LogicOtherOutwhItemBatchDO outwhItemBatchDO = new LogicOtherOutwhItemBatchDO();
//		outwhItemBatchDO.setOtherOutwhItemId(lOtherOutwhItemDO.getId());
//		outwhItemBatchDO.setMaterialsid(lOtherOutwhItemDO.getMaterialsid());
//		outwhItemBatchDO.setCode(lOtherOutwhItemDO.getCode());
//		outwhItemBatchDO.setName(lOtherOutwhItemDO.getName());
//		outwhItemBatchDO.setSpec(lOtherOutwhItemDO.getSpec());
//		outwhItemBatchDO.setJunit(lOtherOutwhItemDO.getJunit());
//		outwhItemBatchDO.setUnit(lOtherOutwhItemDO.getUnit());
//		outwhItemBatchDO.setUnitconvert(lOtherOutwhItemDO.getUnitconvert());
//		if(lOtherOutwhItemDO.getItemBatch() != null & !"".equals(lOtherOutwhItemDO.getItemBatch())){
//			request.setAttribute("itemBatchDOs", lOtherOutwhItemDO.getItemBatch());
//		}
//		request.setAttribute("whCode", whCode);
//		request.setAttribute("billstatus", billstatus);
//		request.setAttribute("outwhItemBatchDO", JSONArray.toJSONString(outwhItemBatchDO));
//		return ITEMMANAGER;
//	}
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		 Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"wh_code");
		 scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		 if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			 scs.setOrderBy("dctime desc");
		SearchResult result = logicOtherOutwhService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

//	@RequestMapping(value = "/itemlist")
//	public void itemlist(HttpServletRequest request, HttpServletResponse response)
//			throws Exception {
//		String itembatch =request.getParameter("itembatch");
//		List<LogicOtherOutwhItemBatchDO> itemBatchDOs=null;
//		if(itembatch != null  && !"".equals(itembatch)){
//			itemBatchDOs=JSONArray.parseArray(new String(itembatch.getBytes("ISO-8859-1"),"UTF-8"), LogicOtherOutwhItemBatchDO.class);
//		}
//		SearchResult result= new SearchResult();
//		if(itemBatchDOs != null && itemBatchDOs.size()>0){
//			result.setRecordCount(itemBatchDOs.size());
//			result.setResults(itemBatchDOs);
//		}
//		 OutJsonGrid out=new OutJsonGrid(result);
//		 out.printJSon(response);
//		
//	}
	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("viewflag", "add");
			model.addAttribute("xx", 1);// 标示能不能修改内容
		} else {
			LogicOtherOutwhDO oi = logicOtherOutwhService.findListById(id);
			//List<LogicOtherOutwhItemDO> loiilist = logicOtherOutwhItemService.findByPid(id);
			if (oi.getBillStatus() == 1) {
				model.addAttribute("xx", 1);// 标示能不能修改内容
				model.addAttribute("viewflag", "tijiao");
			} else {
				model.addAttribute("xx", 2);// 标示能不能修改内容
			}
			if (oi.getBillStatus() == 3) {
				model.addAttribute("viewflag", "tongguo");
			}
			if (oi.getBillStatus() == 2) {
				model.addAttribute("viewflag", "shenpi");
			}
			model.addAttribute("oi", oi);
			//model.addAttribute("axxJson", loiilist);
		}
		return EIDT;
	}
	
	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			List<LogicOtherOutwhItemDO> loiilist = logicOtherOutwhItemService.findByPid(id);
			OutJsonGrid out = new OutJsonGrid(loiilist);
			out.printJSon(response);
		}
	} 

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicOtherOutwhDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
//		String itemdata = request.getParameter("addressInfos");
//		if(itemdata==null&&"".equals(itemdata)){
//			bodyMap.isFail();
//			bodyMap.configWebInfor("物料不能为空！");
//			return bodyMap.modelMap;
//		}else{
//				List<LogicOtherOutwhItemDO> outwhItemDOs  = JSONArray.parseArray(itemdata, LogicOtherOutwhItemDO.class);
//				for (LogicOtherOutwhItemDO itemDO : outwhItemDOs) {
//					if("".equals(itemDO.getItemBatch()) || itemDO.getItemBatch()==null){
//						bodyMap.isFail();
//						bodyMap.configWebInfor("物料编码为:'"+itemDO.getCode()+"'没有分批次填写数据！");
//						return bodyMap.modelMap;
//					}else{
//						List<LogicOtherOutwhItemBatchDO> batchDOs  = JSONArray.parseArray(itemDO.getItemBatch(), LogicOtherOutwhItemBatchDO.class);
//						if(batchDOs==null || batchDOs.size() <= 0){
//							bodyMap.isFail();
//							bodyMap.configWebInfor("物料编码为:'"+itemDO.getCode()+"'没有分批次填写数据！");
//							return bodyMap.modelMap;
//						}
//					}
//				}
//			
//		}
		try {
			logicOtherOutwhService.outSave(curAP,request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (SyBaseException e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields = ids.split(",");
			for (String field : fields) {
				LogicOtherOutwhDO lout=logicOtherOutwhService.findListById(field);
				lout.setRemoved(1);
				logicOtherOutwhService.update(lout, SessionUtil.getUser(request));
//				logicOtherOutwhService.delete(field,
//						SessionUtil.getUser(request));
			}

			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	@RequestMapping(value = "/getStorageQuaitity")
	@ResponseBody
	public String getStorageQuaitity(HttpServletRequest request, HttpServletResponse response ,Model model)throws Exception{
		String maCode = request.getParameter("maCode");
		String whCode = request.getParameter("whCode");
		String hubCode = request.getParameter("hubCode");
		String status = request.getParameter("status");
		String batch = request.getParameter("batch");
		return logicWhAffStockService.getStorageQuaitity(maCode, whCode, hubCode, status, batch)+"";
	}
	
	@RequestMapping(value = "/getWhBatch")
	@ResponseBody
	public List<String> getWhBatch(HttpServletRequest request, HttpServletResponse response ,Model model)throws Exception{
		String maCode = request.getParameter("maCode");
		String whCode = request.getParameter("whCode");
		String hubCode = request.getParameter("hubCode");
		Map<String, String> map=new HashMap<String, String>();
		if(maCode!=null&&!"".equals(maCode)){
			map.put("code", maCode);
		}
		if(whCode!=null&&!"".equals(whCode)){
			map.put("whcode", whCode);
		}
		if(hubCode!=null&&!"".equals(hubCode)){
			map.put("hubcode", hubCode);
		}
		return logicWhAffStockService.findbatch(map);
	}
	
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String, String> map=new HashMap<String, String>();
		try{
			String ids = request.getParameter("rows");
			if(ids!=null&&!"".equals(ids)){
				LogicOtherOutwhDO lot=logicOtherOutwhService.findListById(ids);
				if(lot.getId()!=null){
					lot.setBillStatus(Short.parseShort("1"));
					logicOtherOutwhService.update(lot, SessionUtil.getUser(request));
					map.put("msg", "SUCCESS");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			map.put("msg", "ERROR");
		}
		return map;
	}
}
